﻿using System;

namespace PoolKit
{
    /// <summary>
    /// Encapsulates functionality needed to implement the Disposable pattern in the basic configuration
    /// (without a finalizer, that is)
    /// </summary>
    internal class DisposableCookie
    {
        private readonly string _objectDisposedErrorMessage;
        private bool _disposed;

        public DisposableCookie(string objectDisposedErrorMessage)
        {
            if (objectDisposedErrorMessage == null) throw new ArgumentNullException("objectDisposedErrorMessage");

            _objectDisposedErrorMessage = objectDisposedErrorMessage;
        }

        /// <summary>
        /// </summary>
        /// <exception cref="ObjectDisposedException"></exception>
        public void ThrowIfDisposed()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(_objectDisposedErrorMessage);
            }
        }

        public void MarkAsDisposed()
        {
            _disposed = true;
        }

        public bool IsDisposed
        {
            get { return _disposed; }
        }
    }
}